<template>
  <div class="admin-dashboard">
    <!-- 页面标题 -->
    <div class="dashboard-header">
      <h1>管理员控制台</h1>
      <p>万象鉴真系统管理后台</p>
    </div>

    <!-- 统计卡片 -->
    <div class="stats-cards">
      <div class="stats-card">
        <div class="card-icon user-icon">
          <i class="el-icon-user"></i>
        </div>
        <div class="card-content">
          <h3>{{ totalUsers }}</h3>
          <p>总用户数</p>
        </div>
      </div>
      <div class="stats-card">
        <div class="card-icon detection-icon">
          <i class="el-icon-view"></i>
        </div>
        <div class="card-content">
          <h3>{{ totalDetections }}</h3>
          <p>总检测次数</p>
        </div>
      </div>
      <div class="stats-card">
        <div class="card-icon rumor-icon">
          <i class="el-icon-warning"></i>
        </div>
        <div class="card-content">
          <h3>{{ totalRumors }}</h3>
          <p>谣言库条目</p>
        </div>
      </div>
      <div class="stats-card">
        <div class="card-icon fraud-icon">
          <i class="el-icon-warning-outline"></i>
        </div>
        <div class="card-content">
          <h3>{{ totalFrauds }}</h3>
          <p>诈骗案例</p>
        </div>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="dashboard-content">
      <!-- 左侧内容 -->
      <div class="left-content">
        <!-- 系统监控 -->
        <el-card class="monitor-card">
          <template #header>
            <div class="card-header">
              <span>系统监控</span>
              <el-button type="text" @click="viewSystemMonitoring">查看详情</el-button>
            </div>
          </template>
          <div class="monitor-metrics">
            <div class="metric">
              <div class="metric-label">CPU使用率</div>
              <el-progress :percentage="cpuUsage" :color="getProgressColor(cpuUsage)" />
            </div>
            <div class="metric">
              <div class="metric-label">内存使用率</div>
              <el-progress :percentage="memoryUsage" :color="getProgressColor(memoryUsage)" />
            </div>
            <div class="metric">
              <div class="metric-label">磁盘使用率</div>
              <el-progress :percentage="diskUsage" :color="getProgressColor(diskUsage)" />
            </div>
          </div>
        </el-card>

        <!-- 数据统计图表 -->
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>检测量统计</span>
              <el-button type="text" @click="viewUsageStatistics">查看详情</el-button>
            </div>
          </template>
          <div class="chart-container">
            <div ref="chartContainer" class="chart"></div>
          </div>
        </el-card>
      </div>

      <!-- 右侧内容 -->
      <div class="right-content">
        <!-- 快速操作 -->
        <el-card class="quick-actions">
          <template #header>
            <span>快速操作</span>
          </template>
          <div class="actions-grid">
            <div class="action-item" @click="manageUsers">
              <i class="el-icon-user"></i>
              <span>用户管理</span>
            </div>
            <div class="action-item" @click="manageRumors">
              <i class="el-icon-warning"></i>
              <span>谣言管理</span>
            </div>
            <div class="action-item" @click="manageFrauds">
              <i class="el-icon-warning-outline"></i>
              <span>诈骗案例</span>
            </div>
            <div class="action-item" @click="manageModels">
              <i class="el-icon-cpu"></i>
              <span>模型管理</span>
            </div>
            <div class="action-item" @click="viewAnalytics">
              <i class="el-icon-data-analysis"></i>
              <span>数据分析</span>
            </div>
            <div class="action-item" @click="systemSettings">
              <i class="el-icon-setting"></i>
              <span>系统设置</span>
            </div>
          </div>
        </el-card>

        <!-- 最新动态 -->
        <el-card class="activities">
          <template #header>
            <span>最新动态</span>
          </template>
          <div class="activity-list">
            <div class="activity-item" v-for="activity in recentActivities" :key="activity.id">
              <div class="activity-icon">
                <i :class="activity.icon"></i>
              </div>
              <div class="activity-content">
                <div class="activity-title">{{ activity.title }}</div>
                <div class="activity-time">{{ activity.time }}</div>
              </div>
            </div>
          </div>
        </el-card>

        <!-- 系统状态 -->
        <el-card class="system-status">
          <template #header>
            <span>系统状态</span>
          </template>
          <div class="status-list">
            <div class="status-item" v-for="status in systemStatus" :key="status.name">
              <div class="status-label">{{ status.name }}</div>
              <div class="status-value" :class="status.status">
                {{ status.value }}
              </div>
            </div>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import * as echarts from 'echarts'

export default {
  name: 'AdminDashboard',
  setup() {
    const router = useRouter()
    const chartContainer = ref(null)
    let chart = null

    // 统计数据
    const totalUsers = ref(1248)
    const totalDetections = ref(25689)
    const totalRumors = ref(3456)
    const totalFrauds = ref(1234)

    // 系统监控数据
    const cpuUsage = ref(45)
    const memoryUsage = ref(62)
    const diskUsage = ref(34)

    // 最新动态
    const recentActivities = ref([
      {
        id: 1,
        title: '用户张三进行了文本检测',
        time: '5分钟前',
        icon: 'el-icon-document'
      },
      {
        id: 2,
        title: '检测到新的谣言信息',
        time: '10分钟前',
        icon: 'el-icon-warning'
      },
      {
        id: 3,
        title: '系统进行了例行备份',
        time: '1小时前',
        icon: 'el-icon-folder'
      },
      {
        id: 4,
        title: '模型训练完成',
        time: '2小时前',
        icon: 'el-icon-cpu'
      },
      {
        id: 5,
        title: '用户李四提交了反馈',
        time: '3小时前',
        icon: 'el-icon-message'
      }
    ])

    // 系统状态
    const systemStatus = ref([
      { name: '数据库连接', value: '正常', status: 'normal' },
      { name: 'AI模型服务', value: '正常', status: 'normal' },
      { name: '文件存储', value: '正常', status: 'normal' },
      { name: '邮件服务', value: '异常', status: 'warning' },
      { name: '备份服务', value: '正常', status: 'normal' }
    ])

    // 初始化图表
    const initChart = () => {
      if (!chartContainer.value) return

      chart = echarts.init(chartContainer.value)
      
      const option = {
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['文本检测', '图像检测', '视频检测', '音频检测']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '文本检测',
            type: 'line',
            stack: '总量',
            data: [120, 132, 101, 134, 90, 230, 210]
          },
          {
            name: '图像检测',
            type: 'line',
            stack: '总量',
            data: [220, 182, 191, 234, 290, 330, 310]
          },
          {
            name: '视频检测',
            type: 'line',
            stack: '总量',
            data: [150, 232, 201, 154, 190, 330, 410]
          },
          {
            name: '音频检测',
            type: 'line',
            stack: '总量',
            data: [320, 332, 301, 334, 390, 330, 320]
          }
        ]
      }

      chart.setOption(option)
    }

    // 获取进度条颜色
    const getProgressColor = (percentage) => {
      if (percentage > 80) return '#f56c6c'
      if (percentage > 60) return '#e6a23c'
      return '#67c23a'
    }

    // 导航方法
    const viewSystemMonitoring = () => {
      router.push('/admin/system/monitoring')
    }

    const viewUsageStatistics = () => {
      router.push('/admin/data-analysis/usage-statistics')
    }

    const manageUsers = () => {
      router.push('/admin/system/users')
    }

    const manageRumors = () => {
      router.push('/admin/content/rumor-database')
    }

    const manageFrauds = () => {
      router.push('/admin/content/fraud-cases')
    }

    const manageModels = () => {
      router.push('/admin/model/training')
    }

    const viewAnalytics = () => {
      router.push('/admin/data-analysis/usage-statistics')
    }

    const systemSettings = () => {
      router.push('/admin/system/monitoring')
    }

    // 模拟数据更新
    const updateData = () => {
      // 更新系统监控数据
      cpuUsage.value = Math.floor(Math.random() * 100)
      memoryUsage.value = Math.floor(Math.random() * 100)
      diskUsage.value = Math.floor(Math.random() * 100)
    }

    onMounted(() => {
      initChart()
      
      // 定时更新数据
      const interval = setInterval(updateData, 5000)
      
      // 清理定时器
      onUnmounted(() => {
        clearInterval(interval)
        if (chart) {
          chart.dispose()
        }
      })
    })

    return {
      totalUsers,
      totalDetections,
      totalRumors,
      totalFrauds,
      cpuUsage,
      memoryUsage,
      diskUsage,
      recentActivities,
      systemStatus,
      chartContainer,
      getProgressColor,
      viewSystemMonitoring,
      viewUsageStatistics,
      manageUsers,
      manageRumors,
      manageFrauds,
      manageModels,
      viewAnalytics,
      systemSettings
    }
  }
}
</script>

<style scoped>
.admin-dashboard {
  padding: 20px;
}

.dashboard-header {
  margin-bottom: 30px;
}

.dashboard-header h1 {
  font-size: 32px;
  color: #333;
  margin-bottom: 10px;
}

.dashboard-header p {
  font-size: 16px;
  color: #666;
}

.stats-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  margin-bottom: 30px;
}

.stats-card {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  display: flex;
  align-items: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.2s;
}

.stats-card:hover {
  transform: translateY(-2px);
}

.card-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 20px;
  font-size: 24px;
  color: #fff;
}

.user-icon {
  background: linear-gradient(45deg, #409eff, #66d9ef);
}

.detection-icon {
  background: linear-gradient(45deg, #67c23a, #95de64);
}

.rumor-icon {
  background: linear-gradient(45deg, #e6a23c, #ffd666);
}

.fraud-icon {
  background: linear-gradient(45deg, #f56c6c, #ff9c6e);
}

.card-content h3 {
  font-size: 28px;
  color: #333;
  margin-bottom: 5px;
}

.card-content p {
  font-size: 14px;
  color: #666;
}

.dashboard-content {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 20px;
}

.left-content {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.right-content {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.monitor-card {
  height: 200px;
}

.monitor-metrics {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.metric {
  display: flex;
  align-items: center;
  gap: 15px;
}

.metric-label {
  width: 100px;
  font-size: 14px;
  color: #666;
}

.chart-card {
  height: 300px;
}

.chart-container {
  height: 250px;
}

.chart {
  width: 100%;
  height: 100%;
}

.quick-actions {
  height: 250px;
}

.actions-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 15px;
}

.action-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 15px;
  border: 1px solid #e6e6e6;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
}

.action-item:hover {
  background: #f0f9ff;
  border-color: #409eff;
  color: #409eff;
}

.action-item i {
  font-size: 24px;
  margin-bottom: 8px;
}

.action-item span {
  font-size: 12px;
}

.activities {
  height: 300px;
}

.activity-list {
  max-height: 250px;
  overflow-y: auto;
}

.activity-item {
  display: flex;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid #f0f0f0;
}

.activity-item:last-child {
  border-bottom: none;
}

.activity-icon {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #409eff;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  font-size: 14px;
}

.activity-content {
  flex: 1;
}

.activity-title {
  font-size: 14px;
  color: #333;
  margin-bottom: 4px;
}

.activity-time {
  font-size: 12px;
  color: #999;
}

.system-status {
  height: 250px;
}

.status-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.status-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 15px;
  background: #f8f9fa;
  border-radius: 6px;
}

.status-label {
  font-size: 14px;
  color: #666;
}

.status-value {
  font-size: 14px;
  font-weight: 500;
  padding: 4px 8px;
  border-radius: 4px;
}

.status-value.normal {
  background: #f0f9ff;
  color: #67c23a;
}

.status-value.warning {
  background: #fef0e6;
  color: #e6a23c;
}

.status-value.error {
  background: #fef0f0;
  color: #f56c6c;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .dashboard-content {
    grid-template-columns: 1fr;
  }
  
  .actions-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  .stats-cards {
    grid-template-columns: 1fr;
  }
  
  .actions-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style> 